﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HSDc.RingleFlow.Api.Service;
using System.Configuration;
using System.Reflection;

namespace HSDc.RingleFlow.Api.Factory
{
    public class WorkflowEngineFactory: IWorkflowEngine
    {
        private static WorkflowEngineFactory _singleton = null;

        private static readonly object padlock = new object();

        public static WorkflowEngineFactory GetInstance()
        {
            lock (padlock)
            {
                if (_singleton == null)
                    _singleton = new WorkflowEngineFactory();
                return _singleton;
            }
        }

        private IRepositoryService _RepositoryService = null;
        private IRuntimeService _RuntimeService = null;
        private ITaskService _TaskService = null;
        private IHistoryService _HistoryService = null;
        private IOrganisationService _OrganisationService = null;

        private WorkflowEngineFactory() { }
        IRepositoryService IWorkflowEngine.RespositoryService
        {
            get
            {
                if (_RepositoryService == null)
                    _RepositoryService = (IRepositoryService)GetObject(ServiceType.Repoistory);
                return _RepositoryService;
            }
        }

        IRuntimeService IWorkflowEngine.RuntimeService
        {
            get
            {
                if (_RuntimeService == null)
                    _RuntimeService = (IRuntimeService)GetObject(ServiceType.Runtime);
                return _RuntimeService;
            }
        }

        ITaskService IWorkflowEngine.TaskService
        {
            get
            {
                if (_TaskService == null)
                    _TaskService = (ITaskService)GetObject(ServiceType.Task);
                return _TaskService;
            }
        }

        IHistoryService IWorkflowEngine.HistoryService
        {
            get
            {
                if (_HistoryService == null)
                    _HistoryService = (IHistoryService)GetObject(ServiceType.History);
                return _HistoryService;
            }
        }

        IOrganisationService IWorkflowEngine.OrgaisationService
        {
            get 
            {
                if (_OrganisationService == null)
                    _OrganisationService = (IOrganisationService)GetObject(ServiceType.Organisation);
                return _OrganisationService;
            }
        }

        private Object GetObject(ServiceType type)
        {
            FactoryHandler configSection = new FactoryHandler();
            string configPath = configSection.GetType().Assembly.CodeBase.Substring(8).Replace("/", "\\");
            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(configPath);
            string configName = "ringleFlow/"+type.ToString();
            string className = (string)config.GetSection(configName).ElementInformation.Properties["className"].Value;
            AssemblyName myAssemblyName = new AssemblyName();
            myAssemblyName.Name = config.GetSection(configName).ElementInformation.Properties["dllName"].Value.ToString();
            myAssemblyName.CodeBase = configSection.GetType().Assembly.CodeBase;
            //myAssemblyName.CodeBase = myAssemblyName.CodeBase.Substring(0, myAssemblyName.CodeBase.LastIndexOf("/")) +"/"+ myAssemblyName.Name + ".DLL";
            Assembly instance = Assembly.Load(myAssemblyName);
            return instance.CreateInstance(className);
        }
    }

    internal enum ServiceType { Repoistory, Runtime, Task, History, Organisation }
}
